package durak.server.servlets;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

import durak.server.control.Game;
import durak.server.control.GamesManager;
import durak.server.control.PlayerControl;
import durak.server.control.PlayerControlAutomatic;
import durak.server.control.ReadFromXmlFile;
import durak.server.model.Player.playerType;
import durak.utils.ServletUtils;
import durak.utils.SessionUtils;

import java.io.IOException;
import java.io.StringReader;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class NewGame
 */
@WebServlet(name = "newGame", urlPatterns = { "/newGame" })
public class NewGame extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Game game;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewGame() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#getServletInfo()
	 */
	public String getServletInfo() {
		// TODO Auto-generated method stub
		return null; 
	}
	
	private void initAutoPlayers(int autoPlayers, Game currentGame) {
		for (int i = 0; autoPlayers > i; i++) {
			PlayerControl autoPlayer = new PlayerControlAutomatic(currentGame);
			autoPlayer.getPlayer().setType(playerType.COMPUTER);
			autoPlayer.getPlayer().setName("AutoPlayer" + i);
			currentGame.addPlayer(autoPlayer);
		}
	}
	
	private void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Gson gson = new Gson();
		String gameName = request.getParameter("name");
		Integer humanPlayers = gson.fromJson(request.getParameter("human"), Integer.class);
		Integer autoPlayers = gson.fromJson(request.getParameter("auto"), Integer.class);
		GamesManager gameManager = ServletUtils.getGamesManager(getServletContext());
		if(gameName != null) {
			System.out.println("New game name is: " + gameName + " with " + humanPlayers + " human palyers and " + autoPlayers + " auto players");
			Game game = new Game(gameName,humanPlayers, autoPlayers, false);
			initAutoPlayers(autoPlayers, game);
			gameManager.addGame(game);
		}
		String jsonResponse = gson.toJson(gameManager.getGamesDescriptions());
		response.getWriter().print(jsonResponse);
		response.getWriter().flush();
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		handleRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		handleRequest(request, response);
	}

}
